package com.quectel.core.module.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.device.dao.DeviceCategoryDao;
import com.quectel.core.module.device.dto.DeviceCategoryDto;
import com.quectel.core.module.device.entity.DeviceCategoryEntity;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.redis.RedisUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.quectel.core.constants.RedisCacheConstants.PROJECT_NAME;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:10:19
 */
@DubboService
public class DeviceCategoryServiceImpl implements DeviceCategoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceCategoryServiceImpl.class);
    /**
     * 设备种类缓存 根据id
     */
    public static final String DEVICE_CATEGORY_CACHE = PROJECT_NAME + "DEVICE_CATEGORY_CACHE";
    /**
     * 设备种类缓存 根据code
     */
    public static final String DEVICE_CATEGORY_CODE_CACHE = PROJECT_NAME + "DEVICE_CATEGORY_CODE_CACHE";

    @Autowired
    private DeviceCategoryDao deviceCategoryDao;


    @Override
    public List<DeviceCategoryDto> queryList(Map<String, Object> params) {
        List<DeviceCategoryEntity> list = deviceCategoryDao.queryList(params);
        return CopyUtils.copyList(list, DeviceCategoryDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deviceCategoryDao.queryTotal(params);
    }

    @Override
    public Long save(DeviceCategoryDto dto) {
        DeviceCategoryEntity entity = CopyUtils.copyObj(dto, DeviceCategoryEntity.class);
        entity.setId(Snowflake.nextId());
        deviceCategoryDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeviceCategoryDto dto) {

        RedisUtils.del(DEVICE_CATEGORY_CACHE + dto.getId());

        DeviceCategoryEntity entity = CopyUtils.copyObj(dto, DeviceCategoryEntity.class);
        deviceCategoryDao.updateById(entity);
    }

    @Override
    public DeviceCategoryDto selectById(Long id) {

        DeviceCategoryEntity entity = deviceCategoryDao.selectById(id);
        DeviceCategoryDto categoryDto = CopyUtils.copyObj(entity, DeviceCategoryDto.class);

        return categoryDto;
    }

    @Override
    public DeviceCategoryDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEVICE_CATEGORY_CACHE + id,
                5 * 60
        );
    }

    @Override
    public void deleteById(Long id) {

        RedisUtils.del(DEVICE_CATEGORY_CACHE + id);

        deviceCategoryDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public DeviceCategoryDto selectCacheByCode(String code) {
        return CacheKit.cacheToRedis(
                () -> selectByCode(code)
                ,
                DEVICE_CATEGORY_CODE_CACHE + code,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DeviceCategoryDto selectByCode(String code) {

        return CopyUtils.copyObj(deviceCategoryDao.selectOne(new QueryWrapper<DeviceCategoryEntity>().eq("code", code).last(SqlConstants.MYSQL_LIMIT_ONE)), DeviceCategoryDto.class);
    }
}
